<?php
        if (is_file(__DIR__ . '/../../libs/aliyun-oss-php-sdk-2.6.0/autoload.php')) {
            require_once __DIR__ . '/../../libs/aliyun-oss-php-sdk-2.6.0/autoload.php';
        }
        // if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        //     require_once __DIR__ . '/../vendor/autoload.php';
        // }

use OSS\OssClient;
use OSS\Core\OssException;

class Upload extends CI_Controller {

	var $_login_userid;

	//获得当前登录的用户id
	private function get_login_userid()
	{
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
		if ($this->tank_auth->is_logged_in()) {
			return $this->tank_auth->get_user_id();
		} else {
			return 0;
		}
	}


    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
		// $this->_login_userid = $this->get_login_userid();
		// $this->load->database();
    }



    // function index() {
    //     $this->load->view('upload_form', array('error' => ' '));
    // }

    // function do_upload() {
    // 	//必须建立名为 "upload" 的storage. "test"为存储文件夹。
    //     $config['upload_path'] = 'upload/test';
    //     $config['allowed_types'] = 'gif|jpg|png';
    //     $config['encrypt_name'] = true;
	// 	//$config['file_name'] = '0001.png';
	// 	//$config['max_size'] = '100';
	// 	//$config['max_width'] = '1024';
	// 	//$config['max_height'] = '768';
    //     $this->load->library('upload', $config);
    //     if (!$this->upload->do_upload()) {
    //         print_r($this->upload->display_errors());
    //         $data['error'] = array('error' => $this->upload->display_errors());
    //         //$this->load->view('upload_form', $data);		
	// 		print_r($data['error']);
    //     } else {
    //         $data = array('upload_data' => $this->upload->data() );
    //         $this->load->view('upload_success', $data);
    //     }
    // }

    function img(){
        var_dump($_FILES);
        var_dump($_POST);
        $mypic = $_FILES["mypic"];
        if(!empty($mypic)){
            $tmp_name = $_FILES['mypic']['tmp_name'];
            $picname = $_FILES['mypic']['name'];
            $picsize = $_FILES['mypic']['size'];
            if ($picsize > 5120000) {
                echo '图片大小不能超过5M';
                exit;
            }
            $type = strstr($picname, '.');
            echo $type;
            if ($type != ".gif" && $type != ".jpg" && $type != ".jpeg" && $type != ".png") {
                echo '图片格式不对！';
                exit;
            }
            $target_filename = $_POST['taskid'] . $type;
            //上传路径
            $target_filepath = "_uploads/img/". $target_filename;
            if(ENVIRONMENT=='testing'||ENVIRONMENT=='production'){
                // 上传到oss
                $this->upload_oss($tmp_name,$target_filename);
            }
            // 将临时文件移到服务器本地
            move_uploaded_file($tmp_name,$target_filepath);

            // // 在服务器端修改photo值
            // $this->load->database();
            // $data = array(
            //     'photo' => $pic_path
            // );
            // $this->db->where('id', $_POST['id']);
            // $this->db->where('uid', $this->_login_userid);
            // $this->db->update('task', $data);
            // if ($this->db->affected_rows() > 0) {
            //     $status = 1;
            // } else {
            //     $status = 0;
            // }

        }
    }
    function ci4(){
        $file = $this->request->getFile('mypic');
        $file->move('/upload/test');
    }
    
    private function upload_oss($source_file,$oss_file){
        
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        $accessKeyId = "LTAI5tDVnNdsRvKFopo2ezyN";
        $accessKeySecret = "AOWUHQx1ZeLvpReN5yGNNR7hLWN5y1";
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        $bucket= "thinktodo-image";
        // <yourObjectName>表示上传文件到OSS时需要指定包含文件后缀，不包含Bucket名称在内的完整路径，例如abc/efg/123.jpg。
        // $object = "123.jpg";
        $object = $oss_file;
        // $content = "Hi, OSS.";
        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
        // $filePath = __DIR__ ."/../../_uploads/img/123.png";

        if (is_file($source_file)) {
            try {
                $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
                // 上传对象
                // $ossClient->putObject($bucket, $object, $content);
                // 上传文件
                $ossClient->uploadFile($bucket, $object, $source_file);
            } catch (OssException $e) {
                print $e->getMessage();
            }
        }else{
            echo "没有这个文件".$source_file;

        }

    }

}

